/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.una.accidentmanagement.view.manipulator;

import br.com.una.accidentmanagement.bo.TicketController;
import br.com.una.accidentmanagement.model.ActionTicket;
import br.com.una.accidentmanagement.model.Ticket;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author santosmgbh
 */
public class TicketTable extends DefaultTableModel{
    
    private List<Ticket> tickets;    
    private TicketController ticketController;           

    public List<Ticket> getTickets() {
        return tickets;
    }

    
    
    public TicketTable() {
        ticketController = new TicketController();
        this.tickets = ticketController.getAllTickets();        
        createColumns();        
    }        
    
    public void refresh(){                        
        cleanDate();
        pushDate();
    }        
    
    public Ticket getTicketByIndex(Integer index){        
        return tickets.get(index);
    }
    
    public void filter(String campo, String valor){
        
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false; //To change body of generated methods, choose Tools | Templates.
    }
    
    public void delete(int index){
        ticketController.deleteTicket(tickets.get(index));   
        refresh();
    }
    
    
    private void cleanDate(){
        int size = getRowCount();
        for(int i = 0; i < size; i++){
            removeRow(0);
        }
    }
    
    private void createColumns(){
        addColumn("Código");
        addColumn("Data");
        addColumn("Descrição");
        addColumn("Usuário");
        addColumn("Prioridade");
        addColumn("Nível");
        addColumn("Status");
    }        
    
    private void pushDate(){                        
        for(Ticket t: tickets){            
            putDate(t);
        }        
    }        
    
    public void putAllDate(List<Ticket> tickets){        
        this.tickets = tickets;
    }
    
    public void putDate(Ticket t){
        addRow(new RowTicket(t.getId().toString(), t.getCreated().toString(), t.getDescription(), t.getRequester().getName(), t.getPriority()+"", t.getLevel()+"", t.getStatus()+""));
    }
    
    class RowTicket extends Vector{        
        RowTicket(String codigo, String creationDate, String description, String userName, String priority, String level, String status) {
            add(codigo);
            add(creationDate);
            add(description);
            add(userName);
            add(priority);
            add(level);
            add(status);            
        }
        
        
        
    }
    
    
}
